package org.demo.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.filechooser.FileFilter;

import org.demo.model.impl.FireHoseLoader;
import org.demo.model.impl.QSaharaServer;
import org.demo.utils.Config;

public class ConfigurationFrame extends BaseFrame {

	private static final long serialVersionUID = -5584396213859149343L;

	private JLabel mQSaharaServerPathLabel;
	private JLabel mFirehosePathLabel;
	private JCheckBox mUseVerboseCheckbox;
	private JCheckBox mResetCheckbox;
	private JCheckBox mEarseCheckbox;

	public ConfigurationFrame() {
		super();
		setTitle(TITLE_CONFIGURATION);
		setSize(480, 540);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		initLayout();
		setVisible(true);
	}

	@Override
	protected void initLayout() {
		this.setLayout(null);
		JLabel QSaharaServerLabel = new JLabel("QSaharaServer");
		QSaharaServerLabel.setFont(titleFont);
		QSaharaServerLabel.setBounds(20, 10, 260, 30);
		this.add(QSaharaServerLabel);
		JButton selectQSaharaServerButton = new JButton("Select QSaharaServer");
		selectQSaharaServerButton.setBounds(300, 10, 160, 30);
		selectQSaharaServerButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				selectQSaharaServer();
			}
		});
		this.add(selectQSaharaServerButton);
		mQSaharaServerPathLabel = new JLabel(getQSaharaServer());
		mQSaharaServerPathLabel.setBounds(20, 50, 440, 30);
		this.add(mQSaharaServerPathLabel);

		JLabel FirehoseLabel = new JLabel("Firehose");
		FirehoseLabel.setFont(titleFont);
		FirehoseLabel.setBounds(20, 100, 260, 30);
		this.add(FirehoseLabel);
		JButton selectFirehoseButton = new JButton("Select Firehose");
		selectFirehoseButton.setBounds(300, 100, 160, 30);
		selectFirehoseButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				selectFirehose();
			}
		});
		this.add(selectFirehoseButton);
		mFirehosePathLabel = new JLabel(getFireHoseLoader());
		mFirehosePathLabel.setBounds(20, 140, 440, 30);
		this.add(mFirehosePathLabel);

		mUseVerboseCheckbox = new JCheckBox("Use Verbose");
		mUseVerboseCheckbox.setBounds(20, 190, 200, 30);
		if (mSettingConfig.get(Config.KEY_USE_VERBOSE) != null
				&& mSettingConfig.get(Config.KEY_USE_VERBOSE).equals("1")) {
			mUseVerboseCheckbox.setSelected(true);
		} else {
			mUseVerboseCheckbox.setSelected(false);
		}
		mUseVerboseCheckbox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				mUseVerboseCheckbox.setSelected(!mUseVerboseCheckbox.isSelected());
				if(mUseVerboseCheckbox.isSelected()) {
					System.out.println("verbose selected");
				}else {
					System.out.println("verbose not selected");
				}
			}
		});
		this.add(mUseVerboseCheckbox);

		mResetCheckbox = new JCheckBox("Reset After Download");
		mResetCheckbox.setBounds(20, 230, 200, 30);
		if (mSettingConfig.get(Config.KEY_RESET_AFTER_DOWNLOAD) != null
				&& mSettingConfig.get(Config.KEY_RESET_AFTER_DOWNLOAD).equals("1")) {
			mResetCheckbox.setSelected(true);
		} else {
			mResetCheckbox.setSelected(false);
		}
		mResetCheckbox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				mResetCheckbox.setSelected(!mResetCheckbox.isSelected());
				if(mResetCheckbox.isSelected()) {
					System.out.println("reset selected");
				}else {
					System.out.println("reset not selected");
				}
			}
		});
		this.add(mResetCheckbox);

		mEarseCheckbox = new JCheckBox("Earse All Before Download");
		mEarseCheckbox.setBounds(20, 270, 200, 30);
		if (mSettingConfig.get(Config.KEY_EARSE_ALL_BEFORE_DOWNLOAD) != null
				&& mSettingConfig.get(Config.KEY_EARSE_ALL_BEFORE_DOWNLOAD).equals("1")) {
			mEarseCheckbox.setSelected(true);
		} else {
			mEarseCheckbox.setSelected(false);
		}
		mEarseCheckbox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				mEarseCheckbox.setSelected(!mEarseCheckbox.isSelected());
				if(mEarseCheckbox.isSelected()) {
					System.out.println("earse selected");
				}else {
					System.out.println("earse not selected");
				}
			}
		});
		this.add(mEarseCheckbox);

		JButton okButton = new JButton("OK");
		okButton.setBounds(40, 450, 190, 30);
		okButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				saveConfig();
				dispose();
			}
		});
		this.add(okButton);

		JButton cancelButton = new JButton("Cancel");
		cancelButton.setBounds(250, 450, 190, 30);
		cancelButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		this.add(cancelButton);
	}

	private void selectQSaharaServer() {
		JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		if (!mQSaharaServerPathLabel.getText().isEmpty()) {
			chooser.setCurrentDirectory(new File(mQSaharaServerPathLabel.getText()).getParentFile());
		}
		chooser.setFileFilter(new FileFilter() {

			@Override
			public String getDescription() {
				return QSaharaServer.QSAHARA_SERVER;
			}

			@Override
			public boolean accept(File f) {
				if (f.isDirectory()) {
					return true;
				}
				return f.getName().equals(QSaharaServer.QSAHARA_SERVER);
			}
		});
		chooser.showOpenDialog(this);
		File file = chooser.getSelectedFile();
		if (file != null) {
			mQSaharaServerPathLabel.setText(file.getAbsolutePath());
		}
	}

	private void selectFirehose() {
		JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		if (!mFirehosePathLabel.getText().isEmpty()) {
			chooser.setCurrentDirectory(new File(mFirehosePathLabel.getText()).getParentFile());
		} else if (!mQSaharaServerPathLabel.getText().isEmpty()) {
			chooser.setCurrentDirectory(new File(mQSaharaServerPathLabel.getText()).getParentFile());
		}
		chooser.setFileFilter(new FileFilter() {

			@Override
			public String getDescription() {
				return FireHoseLoader.FH_LOADER;
			}

			@Override
			public boolean accept(File f) {
				if (f.isDirectory()) {
					return true;
				}
				return f.getName().equals(FireHoseLoader.FH_LOADER);
			}
		});
		chooser.showOpenDialog(this);
		File file = chooser.getSelectedFile();
		if (file != null) {
			mFirehosePathLabel.setText(file.getAbsolutePath());
		}
	}

	private void saveConfig() {
		mSettingConfig.put(Config.KEY_QSAHARASERVER, mQSaharaServerPathLabel.getText());
		mSettingConfig.put(Config.KEY_FIREHOSELOADER, mFirehosePathLabel.getText());
		mSettingConfig.put(Config.KEY_USE_VERBOSE, mUseVerboseCheckbox.isSelected() ? "1" : "0");
		mSettingConfig.put(Config.KEY_RESET_AFTER_DOWNLOAD, mResetCheckbox.isSelected() ? "1" : "0");
		mSettingConfig.put(Config.KEY_EARSE_ALL_BEFORE_DOWNLOAD, mEarseCheckbox.isSelected() ? "1" : "0");
		mSettingConfig.save();
	}

}
